/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.javac;

import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.types.Path;

import ws.quokka.core.plugin_spi.support.Setter;

import ws.quokka.plugin.lifecycle.LifeCycleImpl;

import java.io.File;


/**
 *
 */
public class JavacPlugin extends LifeCycleImpl {
    //~ Methods --------------------------------------------------------------------------------------------------------

    public void compile() {
        doCompile(new Path(getProject()));
    }

    public void testCompile() {
        File compileDir = createProperties("quokka.javac.main.").getFile("destDir"); // Add the compile dir to the path
        doCompile(new Path(getProject(), compileDir.getAbsolutePath()));
    }

    public void doCompile(Path additonalPath) {
        File dir = properties().getFile("destDir");
        utils().mkdir(dir);

        Javac javac = (Javac) getProject().createTask("javac");

        // Set up classpath
        Path classpath = getResources().getPathGroupAsPath("compile");
        classpath.add(additonalPath);

        javac.setClasspath(classpath);
        javac.setIncludeantruntime(false);

        Setter setter = new Setter(properties());
        setter.set(javac,
            new String[] {
                "bootClassPath", "bootClassPathRef", "extDirs", "encoding", "noWarn", "debug", "debugLevel", "optimize",
                "deprecation", "target", "verbose", "depend", "fork", "executable", "memoryInitialSize",
                "memoryMaximumSize", "failOnError", "source", "compiler", "listFiles", "tempDir", "srcDir", "destDir"
            });

        String compilerArgs = properties().getString("compilerArgs", null);

        if (compilerArgs != null) {
            Javac.ImplementationSpecificArgument argument = javac.createCompilerArg();
            argument.setLine(compilerArgs);
        }

        javac.perform();
    }
}
